package logic

import (
	"context"
	"errors"
	"gorm.io/gorm"
	"short-video-dev/mysql"

	"short-video-dev/videorpc/internal/svc"
	"short-video-dev/videorpc/videorpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 个人资料修改
func (l *UserUpdateLogic) UserUpdate(in *videorpc.UserUpdateRequest) (*videorpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.UserInfo
	err := user.FindUserById(mysql.DB, in.UserId)
	if err != nil {
		return nil, errors.New("用户不存在,请先注册")
	}

	userUpdate := &mysql.UserInfo{
		Model:      gorm.Model{ID: user.ID},
		NikeName:   in.NikeName,
		AvatarLink: in.HeadImage,
	}

	err = userUpdate.UserUpdate(mysql.DB)
	if err != nil {
		return nil, errors.New("修改失败")
	}

	return &videorpc.UserUpdateResponse{}, nil
}
